﻿using MD5Hash;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    /// <summary>
    /// 会员DAL
    /// </summary>
    public class Member_DAL
    {
        public Member_DAL(Context context)
        {
            Context = context;
        }

        public Context Context { get; }

        /// <summary>
        /// 会员登录
        /// </summary>
        /// <param name="Name">用户名</param>
        /// <param name="Pwd">密码</param>
        /// <returns></returns>
        public Member_Model Login(string? Name,string?Pwd)
        {
            var m =Context.Member_Model.FirstOrDefault(x=>x.Name==Name);
            if (m == null)
            {
                return new Member_Model { Id = -1 };  //用户名不存在
            }
            else
            {
                if (m.Pwd == Pwd)
                {
                    return m;
                }
                else
                {
                    return new Member_Model { Id = -2 };  //密码错误
                }
            }
        }
        /// <summary>
        /// 会员列表
        /// </summary>
        /// <param name="Name">根据会员姓名（模糊查询）</param>
        /// <param name="Age">根据年龄查询</param>
        /// <returns></returns>
        public List<Member_Model> MemberShow(string?Name,int Age)
        {
            var m=Context.Member_Model.ToList();
            if(!string.IsNullOrEmpty(Name))
            {
                m=m.Where(m=>m.Name.Contains(Name)).ToList();
            }
            if (Age>0)
            {
                m=m.Where(x=>x.Age==Age).ToList();
            }
            return m.ToList();
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="id">选中的Id</param>
        /// <param name="ZT">状态</param>
        /// <returns></returns>
        public int XiouGaiZT(int id, string ZT)
        {
            var m=Context.Member_Model.Find(id);
            m.ZT = ZT;
            return Context.SaveChanges();
        }

    }
}
